package org.gap.service;

import java.util.List;
import java.util.Set;

import org.gap.dao.RolesDAO;
import org.gap.dao.RolesTiersDAO;

import org.gap.domain.Roles;
import org.gap.domain.RolesTiers;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Roles entities
 * 
 */

@Service("RolesService")
@Transactional
public class RolesServiceImpl implements RolesService {

	/**
	 * DAO injected by Spring that manages Roles entities
	 * 
	 */
	@Autowired
	private RolesDAO rolesDAO;

	/**
	 * DAO injected by Spring that manages RolesTiers entities
	 * 
	 */
	@Autowired
	private RolesTiersDAO rolesTiersDAO;

	/**
	 * Instantiates a new RolesServiceImpl.
	 *
	 */
	public RolesServiceImpl() {
	}

	/**
	 * Return all Roles entity
	 * 
	 */
	@Transactional
	public List<Roles> findAllRoless(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Roles>(rolesDAO.findAllRoless(startResult, maxRows));
	}

	/**
	 */
	@Transactional
	public Roles findRolesByPrimaryKey(String rolRole) {
		return rolesDAO.findRolesByPrimaryKey(rolRole);
	}

	/**
	 * Return a count of all Roles entity
	 * 
	 */
	@Transactional
	public Integer countRoless() {
		return ((Long) rolesDAO.createQuerySingleResult("select count(o) from Roles o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing Roles entity
	 * 
	 */
	@Transactional
	public void deleteRoles(Roles roles) {
		rolesDAO.remove(roles);
		rolesDAO.flush();
	}

	/**
	 * Save an existing Roles entity
	 * 
	 */
	@Transactional
	public void saveRoles(Roles roles) {
		Roles existingRoles = rolesDAO.findRolesByPrimaryKey(roles.getRolRole());

		if (existingRoles != null) {
			if (existingRoles != roles) {
				existingRoles.setRolRole(roles.getRolRole());
				existingRoles.setRolDescription(roles.getRolDescription());
				existingRoles.setRolPhysiqueMorale(roles.getRolPhysiqueMorale());
			}
			roles = rolesDAO.store(existingRoles);
		} else {
			roles = rolesDAO.store(roles);
		}
		rolesDAO.flush();
	}

	/**
	 * Delete an existing RolesTiers entity
	 * 
	 */
	@Transactional
	public Roles deleteRolesRolesTierses(String roles_rolRole, String related_rolestierses_rotRole, Integer related_rolestierses_rotTiers) {
		RolesTiers related_rolestierses = rolesTiersDAO.findRolesTiersByPrimaryKey(related_rolestierses_rotRole, related_rolestierses_rotTiers, -1, -1);

		Roles roles = rolesDAO.findRolesByPrimaryKey(roles_rolRole, -1, -1);

		related_rolestierses.setRoles(null);
		roles.getRolesTierses().remove(related_rolestierses);

		rolesTiersDAO.remove(related_rolestierses);
		rolesTiersDAO.flush();

		return roles;
	}

	/**
	 * Save an existing RolesTiers entity
	 * 
	 */
	@Transactional
	public Roles saveRolesRolesTierses(String rolRole, RolesTiers related_rolestierses) {
		Roles roles = rolesDAO.findRolesByPrimaryKey(rolRole, -1, -1);
		RolesTiers existingrolesTierses = rolesTiersDAO.findRolesTiersByPrimaryKey(related_rolestierses.getRotRole(), related_rolestierses.getRotTiers());

		// copy into the existing record to preserve existing relationships
		if (existingrolesTierses != null) {
			existingrolesTierses.setRotRole(related_rolestierses.getRotRole());
			existingrolesTierses.setRotTiers(related_rolestierses.getRotTiers());
			existingrolesTierses.setRotDescriptif(related_rolestierses.getRotDescriptif());
			existingrolesTierses.setRotDateDebut(related_rolestierses.getRotDateDebut());
			existingrolesTierses.setRotDateFin(related_rolestierses.getRotDateFin());
			related_rolestierses = existingrolesTierses;
		}

		related_rolestierses.setRoles(roles);
		roles.getRolesTierses().add(related_rolestierses);
		related_rolestierses = rolesTiersDAO.store(related_rolestierses);
		rolesTiersDAO.flush();

		roles = rolesDAO.store(roles);
		rolesDAO.flush();

		return roles;
	}

	/**
	 * Load an existing Roles entity
	 * 
	 */
	@Transactional
	public Set<Roles> loadRoless() {
		return rolesDAO.findAllRoless();
	}
}
